package algorithm;

public class TwoIntArrSort {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int n1Length = nums1.length;
        int n2Length = nums2.length;
        int sumLength = n1Length + n2Length;
        int[] sortArr = new int[(sumLength) / 2 + 1];
        int n1 = 0;
        int n2 = 0;
        for (int i = 0; i < sortArr.length; i++) {
            if (n1 <= n1Length - 1 && n2 <= n2Length - 1 && nums1[n1] <= nums2[n2]) {
                sortArr[i] = nums1[n1];
                n1++;
            } else if (n1 <= n1Length - 1 && n2 <= n2Length - 1 && nums1[n1] > nums2[n2]) {
                sortArr[i] = nums2[n2];
                n2++;
            } else if (n1 <= n1Length - 1) {
                sortArr[i] = nums1[n1];
                n1++;
            } else if (n2 <= n2Length - 1) {
                sortArr[i] = nums2[n2];
                n2++;
            }
        }
        double ret = 0;
        if (sumLength % 2 == 0) {
            ret = (sortArr[sortArr.length - 1] + sortArr[sortArr.length - 2]) / 2.0;
        } else {
            ret = sortArr[sortArr.length - 1] / 1.0;
        }
        return ret;
    }

    public static void main(String[] args) {
        int[] nums1 = new int[]{1, 2};
        int[] nums2 = new int[]{3, 4};
        TwoIntArrSort clazz = new TwoIntArrSort();
        System.out.println(clazz.findMedianSortedArrays(nums1, nums2));
    }
}
